﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Economo.UI.Views;
using Economo.UI.Presenters;
using Economo.ExceptionHandling;

namespace Economo.WinClient.Controls
{
    public partial class ManageEntities : UserControl , IManageEntitiesView 
    {
        protected ManageEntitiesPresenter _presenter;
        public event EntityUpdateEventHandler EntityUpdate;
        public event EntityDeleteEventHandler EntityDelete;
        public event EntityInsertEventHandler EntityInsert;


        public ManageEntities()
        {
            InitializeComponent();
            gridViewEntities.AutoGenerateColumns = false;
        }
        protected virtual void ManageEntities_Load(object sender, EventArgs e)
        {
            if (_presenter !=null)
                _presenter.OnViewLoaded();
            if (System.Diagnostics.Process.GetCurrentProcess().ProcessName != "devenv")
                _presenter.FillData();
        }
           
        private void gridViewEntities_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (EntityUpdate != null)
                EntityUpdate(e.RowIndex);
        }

        private void toolStripButtonInsert_Click(object sender, EventArgs e)
        {
            if (this.EntityInsert != null)
                EntityInsert();
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            if (gridViewEntities.CurrentCell != null)
            {
                object id = GetEntityIdFromGridRow(gridViewEntities.CurrentCell.RowIndex);
                if (this.EntityDelete != null)
                    EntityDelete(id);
            }
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            _presenter.FillData();
        }

        protected void OnEntityUpdate(DataGridViewCellEventArgs e)
        {
            if (EntityUpdate != null)
                EntityUpdate(e.RowIndex);
        }

        #region IManageEntitiesView
        public virtual object GetEntityIdFromGridRow(int rowIndex)
        {
            byte id;
            if (byte.TryParse(gridViewEntities["Id", rowIndex].Value.ToString(), out id))
                return id;
            else
                return null;
        }

        public object EntitiesList
        {
            get
            {
                return gridViewEntities.DataSource;
                
            }
            set
            {
                DataGridViewColumnCollection columns = gridViewEntities.Columns;
                gridViewEntities.DataSource = null;
                gridViewEntities.DataSource = value;
            }
        }

        public string GetStringColumnFromGridRow(int rowIndex, string columnName)
        {
            return gridViewEntities[columnName, rowIndex].Value.ToString();
        }

        public T GetCellValueFromGridRow<T>(int rowIndex, string columnName)
        {
            if (gridViewEntities[columnName, rowIndex].Value != null)
            {
                T value = (T)Convert.ChangeType(gridViewEntities[columnName, rowIndex].Value, typeof(T));
                return value;
            }
            else
                return default(T);
        }
        #endregion



        
    }
}
